from dailyspents.model import orm
from dailyspents._exceptions import DatabaseException
from sqlalchemy.exceptions import *
from sqlalchemy.ext.selectresults import SelectResults
from GenericController import GenericController
from UserSettingsController import UserSettingsController

class UserController(object):
    
    def __init__(self):
        self.query = orm.session.query(orm.User)
        
    def getByLogin(self, login_):
        return self.query.get_by(login = login_)

    def save(self, user):
        try:
            if user.settings == None:
                user.settings = orm.UserSettings()
            orm.session.save(user)
            orm.session.flush()
        except Exception, e:
            orm.session.expunge(user)
            raise e
            raise DatabaseException('Can\'t save the item.')
